package org.javaboy.jpa_demo.dao;

import org.javaboy.jpa_demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserDao extends JpaRepository<User, Integer> {

    /**
     * 根据用户地址查询用户
     * @param address
     * @return
     */
    List<User> findUsersByAddressEquals(String address);

    /**
     * 根据用户地址和用户姓氏进行查询
     * @param address
     * @param username
     * @return
     */
    List<User> findUsersByAddressEqualsOrUsernameStartingWith(String address, String username);

    @Query(value = "select * from t_user where id=(select max(id) from t_user)",nativeQuery = true)
    User maxIdUser();

//    @Query(value = "update t_user set name=?2 where address=?1",nativeQuery = true)
    @Query(value = "update t_user set address=:address where name=:username",nativeQuery = true)
    //如果涉及到更新操作，还需要添加 @Modifying 注解
    @Modifying
    int updateAddressByUsername(@Param("address") String address, @Param("username") String username);
}
